package com.shockweb.common.calculate.function;

import com.shockweb.common.calculate.CalculateException;
import com.shockweb.common.calculate.api.IFunction;

import java.math.BigDecimal;
import java.util.List;

/**
 * 求字符串的长度
 * @author pengminghua
 *
 */
public class Len implements IFunction{
	
	/**
	 * @see IFunction#eval(List)
	 */
	public BigDecimal eval(List<Object> params) throws CalculateException {
		try{
			Object value = params.get(0);
			if(value==null){
				throw new NullPointerException();
			}else{
				return new BigDecimal(((String)value).length());
			}
		}catch(Exception e){
			throw new CalculateException(e);
		}
	}


	/**
	 * @see IFunction#getParamCount()
	 */
	public int getParamCount() {
		return 1;
	}
	
	/**
	 * @see IFunction#getName()
	 */
	public String getName() {
		return "len";
	}
}
